<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公路名称"  prop="roadName">
        <el-select v-model="queryParams.roadName" placeholder="请选择公路">
          <el-option
            v-for="item in gongluList"
            :key="item.id + ''"
            :label="item.field2"
            :value="item.field2 + ''"
            :disabled="item.status == 1"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="隧道名称"  prop="tunnelName">
        <el-select v-model="queryParams.tunnelName" placeholder="请选择隧道">
          <el-option
            v-for="item in suidaoList"
            :key="item.id + ''"
            :label="item.field2"
            :value="item.field2 + ''"
            :disabled="item.status == 1"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="里程桩号" prop="mileageNumber">
        <el-input
          v-model="queryParams.mileageNumber"
          placeholder="请输入里程桩号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设施名称" prop="facilityName">
        <el-select v-model="queryParams.facilityName" placeholder="请输入设施名称" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in fieldfacility" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['civil:facility:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['civil:facility:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['civil:facility:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['civil:facility:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="facilityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="facilityId" />
      <el-table-column label="设施名称" align="center" prop="facilityName" />
      <el-table-column label="安装日期" align="center" prop="installationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.installationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产厂家/施工单位" align="center" prop="manufacturerFactory" />
      <el-table-column label="使用年限" align="center" prop="durableYear" />
      <el-table-column label="道路名称" align="center" prop="roadName" />
      <el-table-column label="道路编码" align="center" prop="roadId" />
      <el-table-column label="隧道名称" align="center" prop="tunnelName" />
      <el-table-column label="隧道编码" align="center" prop="tunnelId" />
      <el-table-column label="里程桩号" align="center" prop="mileageNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['civil:facility:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['civil:facility:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改土建结构设施对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="设施名称" prop="facilityName">
          <el-select v-model="form.facilityName" placeholder="请输入设施名称" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in fieldfacility" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="安装日期" prop="installationDate">
          <el-date-picker clearable
                          v-model="form.installationDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择安装日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="生产厂家/施工单位" prop="manufacturerFactory">
          <el-input v-model="form.manufacturerFactory" placeholder="请输入生产厂家" />
        </el-form-item>
        <el-form-item label="使用年限" prop="durableYear">
          <el-input v-model="form.durableYear" placeholder="请输入使用年限" />
        </el-form-item>
        <el-form-item label="公路名称"  prop="roadName">
          <el-select v-model="form.roadName" placeholder="请选择公路">
            <el-option
              v-for="item in gongluList"
              :key="item.id + ''"
              :label="item.field2"
              :value="item.field2 + ''"
              :disabled="item.status == 1"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="隧道名称" prop="tunnelName">
          <el-select v-model="form.tunnelName" placeholder="请选择隧道名称">
            <el-option
              v-for="dict in suidaoList"
              :key="dict.field2 + ''"
              :label="dict.field2"
              :value="dict.field2 + ''"
            ></el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="隧道编码" prop="tunnelId">
            <el-select v-model="form.tunnelId" placeholder="请选择隧道编码">
              <el-option
                v-for="dict in suidaoList"
                :key="dict.field1 + ''"
                :label="dict.field1"
                :value="dict.field1 + ''"
                v-if="dict.field2 == form.tunnelName"
              ></el-option>
            </el-select>
          </el-form-item>
        <el-form-item label="里程桩号" prop="mileageNumber">
          <el-input v-model="form.mileageNumber" placeholder="请输入里程桩号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import { listFacility, getFacility, delFacility, addFacility, updateFacility,exportFacility } from "@/api/civil/facility";
import { listSuidao } from "@/api/biz/suidao";
import { listGonglu } from "@/api/biz/gonglu";
export default {
  name: "Facility",
  data() {
    return {

      //隧道列表
      suidaoList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 土建结构设施表格数据
      facilityList: [],
      //道路列表
      gongluList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roadName: undefined,
        roadId: undefined,
        tunnelName: undefined,
        tunnelId: undefined,
        mileageNumber: undefined,
        facilityName: undefined,
        installationDate: undefined,
        manufacturerFactory: undefined,
        durableYear: undefined
      },
      // 表单参数
      form: {},
      fieldfacility: [{
        "label": "洞口",
        "value": "洞口"
      }, {
        "label": "洞门",
        "value": "洞门"
      },{
        "label": "衬砌",
        "value": "衬砌"
      },{
        "label": "路面",
        "value": "路面"
      },{
        "label": "检修道",
        "value": "检修道"
      },{
        "label": "排水设施",
        "value": "排水设施"
      },{
        "label": "吊顶及预埋件",
        "value": "吊顶及预埋件"
      },{
        "label": "装饰物",
        "value": "装饰物"
      }, {
        "label": "交通标志/标线",
        "value": "交通标志/标线"
      }
      ],
      // 表单校验
      rules: {
        facilityName:[
          {required:true, message:"设施名称不能为空", trigger:"blur"}
        ],
        installationDate:[
          {required:true, message:"请选择安装日期", trigger:"blur"}
        ],
        manufacturerFactory:[
          {required:true, message:"生产厂家不能为空", trigger:"blur"}
        ],
        roadName:[
          {required:true, message:"道路名称不能为空", trigger:"blur"}
        ],
        roadId:[
          {required:true, message:"道路编码不能为空", trigger:"blur"}
        ],
        tunnelName:[
          {required:true, message:"隧道名称不能为空", trigger:"blur"}
        ],
        tunnelId:[
          {required:true, message:"隧道编码不能为空", trigger:"blur"}
        ],
        mileageNumber:[
          {required:true, message:"里程桩号不能为空", trigger:"blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    listSuidao({}).then((response) => {
      this.suidaoList = response.rows;
    });
    listGonglu({}).then((response) => {
      this.gongluList = response.rows;
    });
  },
  methods: {
    /** 查询土建结构设施列表 */
    getList() {
      this.loading = true;
      listFacility(this.queryParams).then(response => {
        this.facilityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        facilityId: null,
        roadName: null,
        roadId: null,
        tunnelName: null,
        tunnelId: null,
        mileageNumber: null,
        facilityName: null,
        installationDate: null,
        manufacturerFactory: null,
        durableYear: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.facilityId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加土建结构设施";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const facilityId = row.facilityId || this.ids
      getFacility(facilityId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改土建结构设施";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.facilityId != null) {
            updateFacility(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFacility(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const facilityIds = row.facilityId || this.ids;
      this.$modal.confirm('是否确认删除土建结构设施编号为"' + facilityIds + '"的数据项？').then(function() {
        return delFacility(facilityIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有土建结构设施数据项？').then(() => {
        this.exportLoading = true;
        return exportFacility(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
